package nl.scalasim
package actors
package traits

import scala.actors.Actor

/**
 * Generic trait indicating that citizens can work here. 
 *
 * @author Yigal Duppen
 * @since Oct 7, 2010
 */
trait WorkplaceActor extends SimulationActor {

  val maxWorkers: Int

  private[traits] override def workers = Amount(_workers.size, maxWorkers)
  
  private var _workers = List[SimulationActor]()

  def handleWorkAt: PartialFunction[Any, Unit] = {
    case WorkAt(worker, _) if _workers.length == maxWorkers => {
      reply(Available(0))
    }

    case WorkAt(worker, _) => {
      _workers :+= worker
      addRelated(worker)
      reply(Available(maxWorkers - _workers.length))
    }
  }
  callbacks :+= handleWorkAt

}